/**
 * note
 * @author Thot
 */

function TransformableBitmap()
{
	TransformableObject.apply(this);
	this._region = new Square();
	this._region.width = 100;
	this._region.height = 100;
	this._region.alpha = 0.5;
	this.setBody(this._region);
	
	this._info = new tomahawk_ns.TextField();
	this._info.defaultTextFormat.fontSize = 8;
	this._info.width = 80;
	this._info.height = 100;
	this._info.x = this._info.y = 10;
	this._info.setTextAlign(tomahawk_ns.TextField.ALIGN_CENTER);
	//this._info.setText("Zone Image              Double-cliquez pour ajouter une image");
	this._info.setText(Localization.getInstance().getLabel("ZONE_IMAGE"));
	this._info.mouseEnabled = false;
	this.addChild(this._info);
}

Tomahawk.registerClass( TransformableBitmap, "TransformableBitmap" );
Tomahawk.extend( "TransformableBitmap","TransformableObject" );

TransformableBitmap.prototype._info 			= null;
TransformableBitmap.prototype._image 			= null;
TransformableBitmap.prototype._region 			= null;
TransformableBitmap.prototype._border 			= null;
TransformableBitmap.prototype._back 			= null;
TransformableBitmap.prototype.border 			= true;
TransformableBitmap.prototype.borderColor 		= "black";
TransformableBitmap.prototype.allowFilters 		= true;
TransformableBitmap.prototype.allowChangeImage  = true;

TransformableBitmap.prototype.clipTop  			= true;
TransformableBitmap.prototype.clipLeft  		= true;
TransformableBitmap.prototype.clipBottom  		= true;
TransformableBitmap.prototype.clipRight  		= true;

TransformableBitmap.prototype._dblClickHandler = function(event)
{
	this._fileReader = new ImageFileReader();
	this._fileReader.addEventListener( EditorEvent.IMAGE_LOADED, this, this._imageLoadHandler );
	this._fileReader.start();
};

TransformableBitmap.prototype._imageLoadHandler = function(event)
{
	var img = event.image;
	var file = event.file;
	var data = ImageResizer.resize ( img );
	var scope = this;
	var event = new EditorEvent(EditorEvent.USER_IMAGE_ADDED,true,true);
	var texture = null;
	var scope = this;

	event.data = new Object();
	
	img.onload = function()
	{
		event.data.data = data;
		event.data.name = file.name;
		
		var specialChars = "!@#$^&%*()+=-[]\/{}|:<>?,° ";
		for (var i = 0; i < specialChars.length; i++) {
			event.data.name = event.data.name.replace(new RegExp("\\" + specialChars[i], 'gi'), '_');
		}
		
		texture =  new tomahawk_ns.Texture();
		texture.data =  img;
		texture.rect = [0,0,img.width,img.height];
		texture.name = event.data.name;
		tomahawk_ns.AssetsManager.getInstance().addTexture(texture,texture.name);
		
		scope.setTexture(texture);
		scope.dispatchEvent(event);
	};
	
	img.src = data;
};

TransformableBitmap.prototype._mouseHandler 	= function(event)
{
	TransformableObject.prototype._mouseHandler.apply(this, [event] );
	if( event.type == tomahawk_ns.MouseEvent.DOUBLE_CLICK && this.allowChangeImage )
	{
		this._dblClickHandler(event);
		return;
	}
	
	this.refresh();
};

TransformableBitmap.prototype.setTexture 		= function(texture)
{
	if( this._info != null )
	{
		this._info.destroy();
		this._info = null;
	}
	
	if( this._image == null )
	{
		this._image = new tomahawk_ns.Bitmap(texture);
	}
	else
	{
		this.removeChild(this._image);
		this._image.texture = texture;
	}
	
	this._image.width = this._region.width;
	this._image.height = this._region.height;
	
	
	this._region.alpha = 1;
	this._image.setMask(this._region);
	this.addChild(this._image);
	this.setBody(this._region);
	
	this.refresh();
};

TransformableBitmap.prototype.resize = function(width,height)
{
	this._region.width = width;
	this._region.height = height;
	this.refresh();
};

TransformableBitmap.prototype.refresh			= function()
{
	if( this._image == null )
		return;
	
	var naturalHeight = this._image.texture.rect[3];
	var naturalWidth = this._image.texture.rect[2];
	var ratioX = this._region.width / naturalWidth;
	var ratioY = this._region.height / naturalHeight;
	var ratio = ( ratioX > ratioY ) ? ratioX : ratioY;
	var width = naturalWidth * ratio;
	var height = naturalHeight * ratio;
	var diffX = this._region.width - width;
	var diffY = this._region.height - height;
	var x = 0; 
	var y = 0;
	
	if( this.clipLeft == false && this.clipRight == false )
	{
		width = this._region.width;
		x = 0;
	}
	else if( this.clipLeft == true && this.clipRight == true )
	{
		x = diffX * 0.5;
	}
	else if( this.clipLeft == true )
	{
		x = diffX;
	}
	else
	{
		x = 0;
	}
	
	if( this.clipTop == false && this.clipBottom == false )
	{
		height = this._region.height;
		y = 0;
	}
	else if( this.clipTop == true && this.clipBottom == true )
	{
		y = diffY * 0.5;
	}
	else if( this.clipTop == true )
	{
		y = diffY;
	}
	else
	{
		y = 0;
	}
	
	this._image.x = x;
	this._image.y = y;
	this._image.width = width; 
	this._image.height = height; 
	
	if( this._image.cacheAsBitmap == true )
	{
		this._image.updateCache();
		this._image.cacheAsBitmap = false;
	}
	
	this._replaceCorners();
};

TransformableBitmap.prototype.resizeByHeight 	= function()
{
	if( this._image == null )
		return;
		
	var naturalHeight = this._image.texture.rect[3];
	var naturalWidth = this._image.texture.rect[2];
	var naturalRatio = naturalHeight / naturalWidth;
	
	this._image.height = this._region.height;
	this._image.width = this._image.height / naturalRatio; 
	
	this._region.width = this._image.width;
	this._region.height = this._image.height;

	this._region.x = this._image.x = this._region.y = this._image.y = 0;
	this.clipTop = this.clipBottom = this.clipLeft = this.clipRight = true;
	this._drawBorderAndBackground();
	this._replaceCorners();
	
	if( this._image.cacheAsBitmap == true )
	{
		this._image.updateCache();
	}
};

TransformableBitmap.prototype.fitSize = function()
{
	if( this._image == null )
		return;
	
	this._image.width = this._region.width;
	this._image.height = this._region.height; 
	
	
	this._region.x = this._image.x = this._region.y = this._image.y = 0;
	this.clipTop = this.clipBottom = this.clipLeft = this.clipRight = false;
	
	this._drawBorderAndBackground();
	this._replaceCorners();
	
	if( this._image.cacheAsBitmap == true )
	{
		this._image.updateCache();
	}
};

TransformableBitmap.prototype.resizeByWidth 	= function()
{
	if( this._image == null )
		return;
		
	var naturalHeight = this._image.texture.rect[3];
	var naturalWidth = this._image.texture.rect[2];
	var naturalRatio = naturalWidth / naturalHeight;
	
	this._image.width = this._region.width;
	this._image.height = this._image.width / naturalRatio; 
	
	this._region.width = this._image.width;
	this._region.height = this._image.height;
	
	this._region.x = this._image.x = this._region.y = this._image.y = 0;
	this.clipTop = this.clipBottom = this.clipLeft = this.clipRight = true;
	this._drawBorderAndBackground();
	this._replaceCorners();
	
	if( this._image.cacheAsBitmap == true )
	{
		this._image.updateCache();
	}
};

TransformableBitmap.prototype.unserialize 		= function(obj)
{
	var texture = tomahawk_ns.AssetsManager.getInstance().getTextureByAlias(obj.textureName);
	
	TransformableObject.prototype.unserialize.apply(this,[obj]);
	this.allowFilters 	= obj.allowFilters;
	this.allowChangeImage = obj.allowChangeImage;
			
	if( texture != null )
	{
		this.setTexture(texture);
		this.setContrast(obj.contrast);
		this.setBrightness(obj.brightness);
	}
};

TransformableBitmap.prototype.serialize 		= function()
{
	var obj = TransformableObject.prototype.serialize.apply(this,[obj]);
	obj.type = "TransformableBitmap";
	obj.textureName = ( this._image == null ) ? null : this._image.texture.name;
	obj.allowFilters = this.allowFilters;
	obj.allowChangeImage = this.allowChangeImage;
	
	return obj;
};

TransformableBitmap.prototype._addFilters = function()
{
	if( this._image == null )
		return;
		
	this._image.filters = null;
	this._image.cacheAsBitmap = false;
	this.alpha = 1;
	
	if( this.allowFilters == false )
		return;
	
	this.alpha = this._alpha;
	
	var filters = new Array();
	var filt = null;
	
	if( this.contrast != 0 )
	{
		filt = new ContrastFilter();
		filt.value = this.contrast;
		filters.push(filt);
	}
	
	if( this.brightness != 0 )
	{
		filt = new BrightnessFilter();
		filt.value = this.brightness;
		filters.push(filt);
	}
	
	
	if( filters.length > 0 )
	{
		this._image.filters = filters;
		this._image.cacheAsBitmap = true;
		this._image.updateCache();
	}
};

TransformableBitmap.prototype.setBrightness = function(value)
{
	TransformableObject.prototype.setBrightness.apply(this,[value]);
	this._addFilters();
};

TransformableBitmap.prototype.setContrast = function(value)
{
	TransformableObject.prototype.setContrast.apply(this,[value]);
	this._addFilters();
};

TransformableBitmap.prototype.setAlpha = function(value)
{
	TransformableObject.prototype.setAlpha.apply(this,[value]);
	this._addFilters();
};

TransformableBitmap.prototype._replaceCorners 	= function()
{
	TransformableObject.prototype._replaceCorners.apply(this);
	
	if( this._info != null && this._region != null )
	{
		this._info.x = ( this._region.width - this._info.width ) >> 1;
		this._info.y = ( this._region.height - this._info.height ) >> 1;
	}
};
